<!--
 * @Author: your name
 * @Date: 2022-01-31 15:18:29
 * @LastEditTime: 2022-02-02 20:01:07
 * @LastEditors: Please set LastEditors
 * @Description: 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 * @FilePath: \vue3-ts-food-admin\src\views\login\login.vue
-->
<template>
  <div class="login-full-cont">
    <div class="login-center-panel">
      <div class="login-form-logo">点餐后台</div>
      <el-form :model="loginData" ref="loginRef">
        <el-form-item
          label-width="80px"
          label="用户名"
          prop="username"
          :rules="[{ required: true, message: '用户名必填' }]"
        >
          <el-input v-model="loginData.username"></el-input>
        </el-form-item>
        <el-form-item
          label-width="80px"
          label="密码"
          prop="password"
          :rules="[{ required: true, message: '密码必填' }]"
        >
          <el-input v-model="loginData.password"></el-input>
          <!-- :model="loginData.password" 不好使就很有意思 -->
        </el-form-item>
      </el-form>
      <div class="login-form-bottom">
        <el-button type="primary" @click="submitForm(loginRef)">登录</el-button>
      </div>
    </div>
  </div>
</template>

<script lang="ts">
import { ElForm, ElFormItem, ElInput, ElButton } from "element-plus";
import { reactive, defineComponent, ref } from "vue";
import { useRouter } from "vue-router";
// import axios from "axios";

import { setToken } from "@/utils/tool";
import { login } from "@/api/cloud";

export default defineComponent({
  setup() {
    const loginData: { username: string; password: string } = reactive({
      username: "",
      password: "",
    });
    const router = useRouter();
    const loginRef = ref<InstanceType<typeof ElForm>>();

    const submitForm = (formRef: InstanceType<typeof ElForm> | undefined) => {
      if (!formRef) {
        return;
      }
      const formData = loginData;
      formRef.validate((valid) => {
        if (valid) {
          console.log("submit!");

          login(formData).then((res) => {
            console.log(res);
            const { data } = res;
            if (res.status === 200) {
              setToken(data.result);
              router.push("/workspace");
            }
          });
        } else {
          console.log("error submit!");
          return false;
        }
      });
    };

    return { loginData, loginRef, submitForm };
  },
});
</script>

<style lang="less" scoped>
.login-form-logo {
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 18px;
  font-weight: 600;
  margin-bottom: 18px;
}
.login-full-cont {
  height: 100vh;
  display: flex;
  align-content: center;
  justify-content: center;
  align-items: center;
  .login-center-panel {
    width: 400px;
    padding: 18px 12px;
    border-radius: 4px;
    box-shadow: 0px 0px 10px 1px rgba(60, 166, 210, 0.3);
    .login-form-bottom {
      display: flex;
      justify-content: center;
    }
  }
}
</style>
